package com.book.controller;

import com.book.common.BaseController;
import com.book.remote.BrowseHistoryRemote;
import com.book.vo.BrowseHistoryVo;
import com.book.vo.ResponseVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @version 1.0
 * @Author Jincheng.Li
 * @Date 2021/5/30 1:15 下午
 * @Project_Name graduation-project
 */
@Controller
@RequestMapping("/browse/history")
public class BrowseHistoryController extends BaseController {
    @Autowired
    private BrowseHistoryRemote browseHistoryRemote;

    @RequestMapping("/list")
    public String list(Model model){
        String uCode = getUserCode();
        List<BrowseHistoryVo> list = browseHistoryRemote.getList(uCode);
        model.addAttribute("list",list);
        return "/user/history.jsp";
    }

    @RequestMapping("/clearAll")
    @ResponseBody
    public ResponseVo clearAll(){
        browseHistoryRemote.delete(getUserCode());
        return ResponseVo.successResultNotData();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseVo delete(Integer id){
        browseHistoryRemote.deleteBrowseHistory(id);
        return ResponseVo.successResultNotData();
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseVo batchDelete(String ids){
        if (StringUtils.isEmpty(ids)){
            return ResponseVo.errRest("参数为空");
        }
        String[] split = ids.split(",");
        for (String s:split
             ) {
            browseHistoryRemote.deleteBrowseHistory(Integer.parseInt(s));
        }
        return ResponseVo.successResultNotData();
    }
}
